package com.ajieee.merge.service.impl;

import com.ajieee.merge.entity.finance.Quote;
import com.ajieee.merge.entity.finance.po.EtfQuotePO;
import com.ajieee.merge.entity.finance.po.StockQuotePO;
import com.ajieee.merge.mapper.EtfQuoteMapper;
import com.ajieee.merge.mapper.StockQuoteMapper;
import com.ajieee.merge.service.QuoteService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ajieee
 * @date 2025-11-02
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class QuoteServiceImpl implements QuoteService {

	private final EtfQuoteMapper etfQuoteMapper;

	private final StockQuoteMapper stockQuoteMapper;

	@Override
	public List<? extends Quote> queryQuotesByTargetCode(String targetCode) {
		List<? extends Quote> quotes = queryEtfQuotes(targetCode);
		if (quotes.isEmpty()) {
			quotes = queryStockQuotes(targetCode);
		}

		return quotes;
	}

	private List<StockQuotePO> queryStockQuotes(String targetCode) {
		LambdaQueryWrapper<StockQuotePO> wrapper = Wrappers.<StockQuotePO>lambdaQuery()
				.eq(StockQuotePO::getStockCode, targetCode);
		return stockQuoteMapper.selectList(wrapper);
	}

	private List<EtfQuotePO> queryEtfQuotes(String targetCode) {
		LambdaQueryWrapper<EtfQuotePO> wrapper = Wrappers.<EtfQuotePO>lambdaQuery()
				.eq(EtfQuotePO::getFundCode, targetCode);
		return etfQuoteMapper.selectList(wrapper);
	}
}
